package m.member.store.action;

import m.common.action.ActionMeta;
import m.common.action.VueResult;
import m.common.model.util.FromQueryList;
import m.html.VueCollectResult;
import m.html.VueTabResult;
import m.html.list.ListSearch;
import m.html.list.SearchType;
import m.html.tab.TabItem;
import m.member.manage.model.ServeType;
import m.member.order.model.OrderDetail;
import m.member.order.model.OrderInfo;
import m.system.exception.MException;
import m.system.util.DateUtil;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;
import manage.action.ManageAction;
import manage.model.AdminLogin;
import manage.model.OrgGroupView;

import java.util.Date;

@ActionMeta(name = "member/store/orderCollect")
public class OrderCollectAction extends CollectAction {
    public VueResult toOrderTab(){
        String menu=getParamsValue("menu","month");
        String title="month".equals(menu)?"订单月度汇总":"订单年度汇总";
        VueTabResult tab=new VueTabResult(title);
        tab.setSubTitle("{{tabs.names[indexKey]}}");
        Date date=new Date();
        if("month".equals(menu)){
            for(int i=0;i<=5;i++){
                Date d=DateUtil.getAddMonth(date,-i);
                String type=DateUtil.format(d,"yyyy-MM");
                String name=DateUtil.format(d,"yyyy年MM月");
                tab.addItems(new TabItem(menu+type,name,actionUrl("toOrderCollect"),
                        new JSONMessage().push("params",new JSONMessage().push("menu",menu).push("type",type))));
            }
        }else{
            for(int i=0;i<=5;i++){
                Date d=DateUtil.getAddMonth(date,-i*12);
                String type=DateUtil.format(d,"yyyy");
                String name=DateUtil.format(d,"yyyy年");
                tab.addItems(new TabItem(menu+type,name,actionUrl("toOrderCollect"),
                        new JSONMessage().push("params",new JSONMessage().push("menu",menu).push("type",type))));
            }
        }
        return tab;
    }
    public VueResult toOrderCollect() throws MException {
        OrgGroupView org=getSessionOrgGroup();
        if(null==org|| ObjectUtil.isEmptyOid(org.getDept()))
            throw new MException(this.getClass(),"无店铺信息");
        String menu=getParamsValue("menu","month");
        String type=getParamsValue("type","");
        String title="month".equals(menu)?"订单月度汇总":"订单年度汇总";
        FromQueryList from=FromQueryList.fromGroup("a", OrderInfo.class);
        from.addCondition("#{a.status}='Y' and #{a.inputDept.oid}=?",org.getDept().getOid());
        VueCollectResult result=getVueCollectResult(title,from,actionUrl("toOrderCollect"));
        result.setHeight(-1);
        result.setDefaultSearchNum(3);
        result.setPageSize(20);
        result.hideHead();
        result.setFunction(true,true);
        result.setMergeCellsInfo(0,1);
        if("month".equals(menu)){
            Date date=DateUtil.format(type,"yyyy-MM");
            result.setSubTitle(DateUtil.format(date,"yyyy年MM月"));
            from.addCondition("date_format(#{a.inputDate},'%Y-%m')='"+ type +"'");
            from.setOrder("date_format(#{a.inputDate},'%d') asc,#{a.payType} asc");
            result.addCol(collectTextColumn("日期","date_format(#{a.inputDate},'%d') dayNum").setDisItems(getDayItem()));
            result.addSearch(ListSearch.field("日期","dayNum", SearchType.STR_EQ).setDisItems(getDayItem()));
        }else{
            Date date=DateUtil.format(type,"yyyy");
            result.setSubTitle(DateUtil.format(date,"yyyy年"));
            from.addCondition("date_format(#{a.inputDate},'%Y')='"+ type +"'");
            from.setOrder("date_format(#{a.inputDate},'%m') asc,#{a.payType} asc");
            result.addCol(collectTextColumn("月份","date_format(#{a.inputDate},'%m') dayNum").setDisItems(getMonthItem()));
            result.addSearch(ListSearch.field("月份","dayNum", SearchType.STR_EQ).setDisItems(getMonthItem()));
        }
        result.addCol(collectTextColumn("服务员", "#{a.serveAdmin.oid} serveAdmin", 100).setSubtotal(true).setDictType(AdminLogin.class));
        result.addCol(collectTextColumn("付款类型", "#{a.payType} payType", 100).setSubtotal(true).setDictType("m.member.order.model.OrderInfo!payType"));
        result.addCol(collectIntegerColumn("订单数量","count(#{a.oid}) orderNum"));
        result.addCol(collectNumberColumn("订单金额","sum(#{a.orderAmount}) orderAmount"));
        result.addCol(collectNumberColumn("优惠金额","sum(#{a.preferentialAmount}-#{a.otherAmount}-#{a.memberDayAmount}) preferentialAmount"));
        result.addCol(collectNumberColumn("实付金额","sum(#{a.payAmount}) payAmount"));
        result.addCol(collectNumberColumn("付款实际金额","sum(#{a.payRealMoney}) payRealMoney"));
        result.addCol(collectNumberColumn("付款赠送金额","sum(#{a.payGiveMoney}) payGiveMoney"));
        result.addSearch(ListSearch.field("服务员","serveAdmin", SearchType.STR_EQ).setDictType("manage.model.AdminLogin"));
        result.addSearch(ListSearch.field("付款类型","payType", SearchType.STR_EQ).setDictType("m.member.order.model.MemberRecharge!type"));

        return result;
    }
    public VueResult toDetailTab(){
        String menu=getParamsValue("menu","month");
        String title="month".equals(menu)?"订单明细月度汇总":"订单明细年度汇总";
        VueTabResult tab=new VueTabResult(title);
        tab.setSubTitle("{{tabs.names[indexKey]}}");
        Date date=new Date();
        if("month".equals(menu)){
            for(int i=0;i<=5;i++){
                Date d=DateUtil.getAddMonth(date,-i);
                String type=DateUtil.format(d,"yyyy-MM");
                String name=DateUtil.format(d,"yyyy年MM月");
                tab.addItems(new TabItem(menu+type,name,actionUrl("toDetailCollect"),
                        new JSONMessage().push("params",new JSONMessage().push("menu",menu).push("type",type))));
            }
        }else{
            for(int i=0;i<=5;i++){
                Date d=DateUtil.getAddMonth(date,-i*12);
                String type=DateUtil.format(d,"yyyy");
                String name=DateUtil.format(d,"yyyy年");
                tab.addItems(new TabItem(menu+type,name,actionUrl("toDetailCollect"),
                        new JSONMessage().push("params",new JSONMessage().push("menu",menu).push("type",type))));
            }
        }
        return tab;
    }
    public VueResult toDetailCollect() throws MException {
        OrgGroupView org=getSessionOrgGroup();
        if(null==org|| ObjectUtil.isEmptyOid(org.getDept()))
            throw new MException(this.getClass(),"无店铺信息");
        String menu=getParamsValue("menu","month");
        String type=getParamsValue("type","");
        String title="month".equals(menu)?"订单明细月度汇总":"订单明细年度汇总";
        FromQueryList from=FromQueryList.fromGroup("d", OrderDetail.class);
        from.leftJoin("a","#{a.oid}=#{d.order.oid}",OrderInfo.class);
        from.addCondition("#{a.status}='Y' and #{a.inputDept.oid}=?",org.getDept().getOid());
        VueCollectResult result=getVueCollectResult(title,from,actionUrl("toDetailCollect"));
        result.setHeight(-1);
        result.setDefaultSearchNum(3);
        result.setPageSize(20);
        result.hideHead();
        result.setFunction(true,true);
        result.setMergeCellsInfo(0,2);
        if("month".equals(menu)){
            Date date=DateUtil.format(type,"yyyy-MM");
            result.setSubTitle(DateUtil.format(date,"yyyy年MM月"));
            from.addCondition("date_format(#{a.inputDate},'%Y-%m')='"+ type +"'");
            from.setOrder("date_format(#{a.inputDate},'%d') asc,#{d.serve.type.name},#{d.serve.title}");
            result.addCol(collectTextColumn("日期","date_format(#{a.inputDate},'%d') dayNum").setDisItems(getDayItem()));
            result.addSearch(ListSearch.field("日期","dayNum", SearchType.STR_EQ).setDisItems(getDayItem()));
        }else{
            Date date=DateUtil.format(type,"yyyy");
            result.setSubTitle(DateUtil.format(date,"yyyy年"));
            from.addCondition("date_format(#{a.inputDate},'%Y')='"+ type +"'");
            from.setOrder("date_format(#{a.inputDate},'%m') asc,#{d.serve.type.name},#{d.serve.title}");
            result.addCol(collectTextColumn("月份","date_format(#{a.inputDate},'%m') dayNum").setDisItems(getMonthItem()));
            result.addSearch(ListSearch.field("月份","dayNum", SearchType.STR_EQ).setDisItems(getMonthItem()));
        }
        result.addCol(collectTextColumn("服务类型", "#{d.serve.type.oid} typeOid", 100).setSubtotal(true).setDictType(ServeType.class));
        result.addCol(collectTextColumn("服务项目标题", "#{d.serve.title} serveTitle", 150).setSubtotal(true));
        result.addCol(collectNumberColumn("数量","sum(#{d.num}) serveNum"));
        result.addCol(collectNumberColumn("金额","sum(#{d.total}) serveTotal"));
        result.addSearch(ListSearch.field("服务员","serveAdmin", SearchType.STR_EQ).setDictType("manage.model.AdminLogin"));
        result.addSearch(ListSearch.field("付款类型","payType", SearchType.STR_EQ).setDictType("m.member.order.model.MemberRecharge!type"));

        return result;
    }
}
